﻿using Entity.DbEntity;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using pTools.cMethods;
using pTools.PrintTable;
using pTools.TuQiangPrint;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace pTools.ChildWin
{
    public partial class ReBarWin : Form
    {
        DBHelper db;

        string barCode = "";
        string type = "";

        DataTable barTable = new DataTable();


        public ReBarWin(string bCode)
        {
            db = new cMethods.DBHelper();
            InitializeComponent();
            if (bCode.Contains(','))
            {
                barCode = bCode.Split(',')[0];
                type = bCode.Split(',')[1];
            }
            else
            {
                barCode = bCode;
            }


            if (!string.IsNullOrWhiteSpace("type"))
            {
                if (type == "sb")
                {
                    labelControl3.Text = "设备信息";
                }
            }
        }

        private void ReBarWin_Load(object sender, EventArgs e)
        {
            try
            {
                this.MaximizeBox = false;
                this.Text = "条码补打窗体";
                loadData();
                tbBarCode.ReadOnly = true;
                tbBarCode.Text = barCode;
                CommonMethods.loadPrintTemplte(cbTemplate);
                CommonMethods.loadPrintDevice(cbPrint);

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
   

        //获取补打的条码信息
        private void loadData()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("select proCode code ,proName name from V_BarCode_Info ");
            sb.Append(" where barCode='" + barCode + "' ");

            barTable = db.getData(sb.ToString(), "");
            if (barTable.Rows.Count > 0)
            {
                lbProduct.Text = string.Format("{0}({1})", barTable.Rows[0]["name"].ToString(), barTable.Rows[0]["code"].ToString());
            }
        }


        private void btnReClose_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.Close();
        }

        private void btnPrint_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                rePrint();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        # region 条码补打
        void rePrint()
        {
            if (string.IsNullOrEmpty(tbBarCode.Text))
            {
                return;
            }

            var barCode = tbBarCode.Text;

            VProductBar bar;

            GetBarCodeContent(barCode, out bar);

            var table = CommonMethods.createPrintTable();
            var r = table.NewRow();
            r["barCode"] = bar.barCode;
            r["proName"] = bar.proName;
            r["yaGuang"] = bar.yaGuang;
            r["proCode"] = bar.proCode;
            r["spec"] = bar.spec;
            r["chengfen"] = bar.chengfen;
            r["maoWeight"] = bar.maoWeight;
            r["proWeight"] = bar.proWeight;
            r["grade"] = bar.grade;
            r["happenDate"] = bar.happenDate;
            r["checkCode"] = bar.checkCode;
            r["checkName"] = bar.checkName;
            r["Department"] = bar.Department;
            r["deptName"] = bar.deptName;
            r["standard"] = bar.standard;
            r["address"] = bar.address;
            r["phone"] = bar.phone;
            table.Rows.Add(r);

            var message = "";
            CommonMethods.DoPrint(table, cbTemplate.Text, cbPrint.Text, ref message);

        }

        //获取条码信息
        void GetBarCodeContent(string barCode, out VProductBar bar)
        {
            var mainSql_template = @"select * from V_BarCode_Info where barCode = '{0}'; ";
            var mainSql = string.Format(mainSql_template, barCode);

            var mainDt = db.getData(mainSql);

            bar = CommonMethods.ConvertTo<VProductBar>(mainDt).First();
        }

        #endregion



    }


}
